package bufferings.lab.server.controller.tq;

import java.util.logging.Logger;

import org.junit.Test;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskOptions.Builder;

public class TqLimit30sTestController extends Controller {
  private static final Logger logger =
    Logger.getLogger(TqLimit30sTestController.class.getName());

  @Test
  public void TQの30秒制限テストを登録() throws Exception {
    Queue queue = QueueFactory.getDefaultQueue();
    queue.add(Builder.url("/tq/tqLimit30sTest"));
  }

  @Override
  public Navigation run() throws Exception {
    String retryCount = this.request.getHeader("X-AppEngine-TaskRetryCount");
    logger.info("X-AppEngine-TaskRetryCount=" + retryCount);
    if (retryCount != null && Integer.parseInt(retryCount) >= 2) {
      return null;
    }

    try {
      Thread.sleep(40000);
    } catch (Exception e) {
      logger.info("[catch]" + e.toString());
      throw e;
    } finally {
      logger.info("[finally]");
    }
    return null;
  }

  // private void nestCatchWait(int nestCount, long wait) throws Exception {
  // try {
  // if (--nestCount <= 0) {
  // Thread.sleep(40000);
  // } else {
  // nestCatchWait(nestCount, wait);
  // }
  // } catch (Exception e) {
  // logger.info("[catch]" + e.toString());
  // throw e;
  // } finally {
  // logger.info("[finally]");
  // Thread.sleep(wait);
  // }
  // }
}
